package com.ray.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyStore;
import java.util.Properties;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;

/**
 * 客户端socket的封装处理方法
 * 
 */
public class ClientCertSocketFactory implements SecureProtocolSocketFactory {

    private static Properties confiProperties;

    public Socket createSocket(Socket arg0, String host, int port, boolean arg3) throws IOException, UnknownHostException {
        return createSocket(host, port);
    }

    public Socket createSocket(String host, int port, InetAddress arg2, int arg3, HttpConnectionParams arg4) throws IOException, UnknownHostException, ConnectTimeoutException {
        return createSocket(host, port);
    }

    public Socket createSocket(String host, int port, InetAddress arg2, int arg3) throws IOException, UnknownHostException {
        return createSocket(host, port);
    }

    /**
     * 通过属性文件加载属性值
     * 
     * @param property
     * @return
     */
    public static String getPath(String property) {
        if (confiProperties == null) {
            synchronized (ClientCertSocketFactory.class) {
                if (confiProperties == null) {
                    confiProperties = new Properties();
                    InputStream is = null;
                    try {
                        is = Thread.currentThread().getContextClassLoader().getResourceAsStream("properties/ecartoon.properties");
                        confiProperties.load(is);
                    } catch (IOException e) {
                    } finally {
                        if (is != null) {
                            try {
                                is.close();
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
        return confiProperties.getProperty(property);
    }

    /**
     * 创建socket
     */
    public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
        String filePath = ClientCertSocketFactory.getPath("certPath");
        SSLSocket theSocket = null;
        try {
            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
            TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
            KeyStore ks = KeyStore.getInstance("JKS");
            KeyStore tks = KeyStore.getInstance("JKS");
            ks.load(new FileInputStream(filePath + "/WS1.jks"), "loyalty".toCharArray());
            tks.load(new FileInputStream(filePath + "/hunanjicui.trust.jks"), "hunanjicui&123".toCharArray());
            kmf.init(ks, "loyalty".toCharArray());
            tmf.init(tks);
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
            SSLSocketFactory socketFactory = sslContext.getSocketFactory();
            theSocket = (SSLSocket) socketFactory.createSocket(host, port);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return theSocket;
    }

}
